<template>
  <div class="card-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form
          ref="form"
          :inline="true"
          label-width="80px"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item label="项目名称">
            <el-input v-model="queryForm.projectName" placeholder="项目名称" />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              native-type="submit"
              type="primary"
              @click="handleQuery"
            >
              查询
            </el-button>
            <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
              添加
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-row :gutter="20">
      <el-col
        v-for="(item, index) in list"
        :key="index"
        :lg="8"
        :md="8"
        :sm="8"
        :xl="6"
        :xs="24"
      >
        <el-card
          :body-style="{ padding: '0px', position: 'static' }"
          shadow="hover"
          @click.native="handleClickCard(item)"
        >
          <div class="card-title">
            {{ item.projectName }}
          </div>
          <div class="card-description">{{ item.projectDescription }}</div>
          <div class="card-datetime">{{ item.createTime }}</div>
        </el-card>
      </el-col>
    </el-row>

    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <add-form ref="add" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { QueryProjectPage } from '@/api/projectConfig/projectInfo'
  import AddForm from './projectInfoComponents/addProjectForm.vue'

  export default {
    name: 'CustomTable',
    components: {
      AddForm,
    },
    data() {
      return {
        height: this.$baseTableHeight(1),
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageIndex: 1,
          pageSize: 20,
          projectName: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleAdd() {
        this.$refs['add'].showForm()
      },
      // 打开配置信息界面
      handleConfig(row) {
        let routeData = this.$router.resolve({
          path: '/projectConfigInfos',
          query: { id: row.id },
        })

        this.$router.push({
          path: '/other/iframe/view',
          query: {
            title: row.projectName,
            url: routeData.href,
          },
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageIndex = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageIndex = 1
        this.fetchData()
      },
      async fetchData() {
        const {
          data: { list, total },
        } = await QueryProjectPage(this.queryForm)
        this.list = list
        this.total = total
      },
      // 点击卡片事件
      handleClickCard(projectInfo) {
        this.$router.push({
          path: '/configInfoIndex',
          query: {
            id: projectInfo.id,
          },
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .card-container {
  }

  .card-title,
  .card-description,
  .card-datetime {
    margin-top: 6px;
    margin-bottom: 6px;
    margin-left: 10px;
    margin-right: 10px;
  }

  .card-description,
  .card-datetime {
    font-size: 14px;
    color: rgb(80, 80, 80);
  }

  .card-title {
    font-size: 16px;
    font-weight: bolder;
  }

  .card-description {
    height: 32px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
</style>
